package com.haredot.controller;

import com.haredot.dto.BindMenuSysDto;
import com.haredot.entity.Menu;
import com.haredot.service.MenuService;
import com.haredot.utils.ValidUtils;
import com.haredot.vo.PageVo;
import com.haredot.vo.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService ;

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('menu:add')")
    public R<?> addMenu(@RequestBody @Validated Menu menu, BindingResult bindingResult) {
        return ValidUtils.toResult(bindingResult, ()-> {
            menu.setCreateTime(LocalDateTime.now());
            return menuService.saveMenu(menu) ;
        }) ;
    }

    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('menu:query', 'menu')")
    public R<PageVo> list(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                          @RequestParam(value = "rows", required = false, defaultValue = "10") Integer rows,
                          @RequestParam(value = "name", required = false) String name,
                          @RequestParam(value = "state", required = false) Boolean state) {

        // 组装 PageVo 对象
        PageVo pageVo = new PageVo(page, rows);
        // 封装查询参数
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);
        params.put("state", state);

        // 调用业务层，分页查询数据
        return menuService.queryMenusByPage(pageVo, params) ;
    }

    @PutMapping("/bindSys/{menuId}")
    @PreAuthorize("hasAuthority('menu:bind:sys')")
    public R<?> bindSys(@PathVariable("menuId")Long menuId, @RequestBody List<Long> sysIds) {

        return menuService.bindSys(menuId, sysIds) ;
    }

    @DeleteMapping("/batch")
    @PreAuthorize("hasAuthority('menu:batch:del')")
    public R<?> batchRemove(@RequestBody List<Long> menuIds) {

        return menuService.batchRemoveMenus(menuIds);
    }

    @PutMapping("/update/{menuId}")
    @PreAuthorize("hasAuthority('menu:update')")
    public R<?> updateMenu(@PathVariable("menuId") Long menuId, @RequestBody @Validated Menu menu, BindingResult bindingResult) {

        return ValidUtils.toResult(bindingResult, ()-> {
            menu.setId(menuId);
            return menuService.updateMenu(menu);
        });
    }

    @GetMapping("/sysBindMenuList/{sysId}")
    @PreAuthorize("hasAnyAuthority('menu:add', 'menu:update')")
    public R<BindMenuSysDto> sysBindMenuList(@PathVariable("sysId") Long sysId) {

        return menuService.querySysBindMenuListBySysId(sysId);
    }

    @DeleteMapping("/del/{menuId}")
    @PreAuthorize("hasAuthority('menu:del')")
    public R<?> remove(@PathVariable("menuId") Long menuId) {

        return menuService.removeMenu(menuId);
    }
}
